package org.jash.sportnews2109.vm

import androidx.lifecycle.MutableLiveData
import org.jash.common.mvvm.BaseViewModel
import org.jash.common.retrofit
import org.jash.sportnews2109.net.APIService

class PhoneViewModel:BaseViewModel() {
    val service by lazy { retrofit.create(APIService::class.java)}
    val tokenLiveData by lazy { MutableLiveData<String>() }
    val verifyLiveData by lazy { MutableLiveData<String>() }
    fun login(map:Map<String, String>) {
        progress(service.loginByPhoneCode(map)) {
            if (it.code == 0) {
                tokenLiveData.postValue(it.data)
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
    fun getVerifyCode(phone:String) {
        progress(service.sendLoginCode(phone)) {
            if (it.code == 0) {
                verifyLiveData.postValue(it.data)
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
}